package com.voxel.sense.infomsg.module.task.execution.report.details;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.SnackbarUtils;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.dialog.DialogCallBack;
import com.example.com.common.view.dialog.IDialog;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.decoration.ReportItemRecylerItemDivider;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des : 任务填报表格界面
 */
public class DetailsReportActivity extends BViewActivity<TaskReportContract.Presenter>
        implements TaskReportContract.View<MultiItemEntity> {
    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.empty_view)
    EmptyView empty_view;

    private ReportAdapter mAdapter;

    private int mStatus;//记录操作的类型


    /**
     * @param formCode 表单code
     * @param status   状态 ：
     *                 TaskReportContract.ALTER_REPORT==1  上传
     *                 TaskReportContract.UP_REPORT==2 修改
     * @param taskNumb 任务编号
     */
    public static void show(Context context, String formCode, int status, String taskNumb) {
        if (TextUtils.isEmpty(formCode) || TextUtils.isEmpty(taskNumb)) {
            return;
        }
        Intent intent = new Intent(context, DetailsReportActivity.class);
        intent.putExtra(IntentConstants.TASK_NUMB, taskNumb);
        intent.putExtra(IntentConstants.TASK_REPORT_STATUS, status);
        intent.putExtra(IntentConstants.TASK_REPORT_CODE, formCode);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_up_report_actvity;
    }

    @Override
    protected TaskReportContract.Presenter initPresenter() {
        return new DetailsTaskReportPresenter(this);
    }


    @Override
    protected void initWidget() {
        super.initWidget();

    }

    @Override
    protected void initData() {
        super.initData();
        //初始化recycler
        mAdapter = new ReportAdapter(null);
        recycle_view.setLayoutManager(new LinearLayoutManager(this));
        recycle_view.addItemDecoration(new ReportItemRecylerItemDivider(R.drawable.recycle_divider_1dp_pink,
                R.drawable.recycle_divider_5dp_pink));
        recycle_view.setAdapter(mAdapter);
        empty_view.bind(recycle_view);
        //初始化表格数据
        mStatus = getIntent().getIntExtra(IntentConstants.TASK_REPORT_STATUS, 0);
        String formCode = getIntent().getStringExtra(IntentConstants.TASK_REPORT_CODE);
        String taskNumb = getIntent().getStringExtra(IntentConstants.TASK_NUMB);

        if (TaskReportContract.UP_REPORT == mStatus) {
            titlebar.setTitleText("上传报表");
            mPresenter.doLoadRePort(formCode);
        } else if (TaskReportContract.ALTER_REPORT == mStatus) {
            titlebar.setTitleText("修改报表");
            mPresenter.doLoadExistRePort(formCode, taskNumb, mStatus);
        } else {
            titlebar.setTitleText("报表详情");
            mPresenter.doLoadExistRePort(formCode, taskNumb, mStatus);
        }
        empty_view.setRefreshBtnVisible(false);
        empty_view.triggerLoading();
    }

    @Override
    protected void initEven() {
        super.initEven();
        if (mStatus != TaskReportContract.LOOK_REPORT) {
            titlebar.setRightTextAndLis("保存", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String taskNumb = getIntent().getStringExtra(IntentConstants.TASK_NUMB);
                    mPresenter.doUpLoadReport(mAdapter.getData(), taskNumb, mStatus);
                }
            });
        }

    }


    @Override
    public void onAdapterDataChanged() {
        if (mAdapter.getItemCount() > 0) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void showSnackbar(String msg) {
        SnackbarUtils.with(recycle_view).setMessage(msg).showWarning();
    }

    @Override
    public void onUpReportSuccess() {
        IDialog.getInstance().showMessageDialog(this, "", "表格上传成功！", new DialogCallBack() {
            @Override
            public void onClick(int what) {
                finish();
            }
        }).setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                finish();
            }
        });
    }


    @Override
    public void onRefreshData(List<MultiItemEntity> data) {
        mAdapter.replaceData(data);
    }

}
